<template>
  <div>
    <van-nav-bar
     title="黑马头条-登录"
      fixed
       placeholder/>
        <van-form @submit="onSubmit">
  <van-field
    v-model="form.mobile"
    label="手机号"
    placeholder="手机号"
    required
    :rules="[{ required: true, message: '请填写手机号', pattern: /^1[3-9]\d{9}$/}]"
  />
  <van-field
    v-model="form.code"
    type="password"
    label="验证码"
    placeholder="验证码"
    required
    :rules="[{ required: true, message: '请填写验证码', pattern: /^\d{6}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button
    round
    block
    type="info"
    native-type="submit"
    :disabled='isLogin'
    loading-text='加载中......'
    :loading='isLogin'
    >提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import { Notify } from 'vant'
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
export default {
  data () {
    return {
      form: {
        mobile: '13888888888',
        code: '246810'
      },
      isLogin: false // 登录按钮的禁用状态
    }
  },
  methods: {
    // 登录的点击事件
    async onSubmit (values) {
      this.isLogin = true
      try {
        const res = await loginAPI({
          ...this.form
        })
        // console.log('res', res)
        setToken(res.data.data.token)
        this.isLogin = false
        localStorage.setItem('refresh_token', res.data.data.refresh_token)
        Notify({ type: 'success', message: '登录成功' })
        this.$router.push({
          path: '/layout'
        })
      } catch (err) {
        this.isLogin = false

        console.error(err)
        Notify({ type: 'danger', message: '登陆失败' })
      }
    }
  }
}
</script>

<style>
</style>
